package com.zzh.takeoutapp.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.zzh.takeoutapp.adapter.OnRecyleItemClick;
import com.zzh.takeoutapp.entity.HomeShopEntity;

import java.util.ArrayList;
import java.util.List;

public abstract class MyBaseAdapter<T, V extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<V> {

    protected List<T> lists = new ArrayList<T>();
    protected Context context;
    protected OnRecyleItemClick lis;

    public MyBaseAdapter(List<T> list, Context context) {
        this.context = context;
        if (list != null) {
            this.lists.addAll(list);
        }
    }


    public void setData(List<T> list) {
        this.lists = list;
        notifyDataSetChanged();
    }

    public void insert(List<T> person, int position) {
        lists.addAll(position, person);
        notifyItemInserted(position);
    }

    public T getItem(int position) {
        if (position < lists.size())
            return lists.get(position);
        else
            return null;
    }

    @Override
    public int getItemCount() {
        return lists != null ? lists.size() : 0;
    }

    public void addOnRecyleItemClick(OnRecyleItemClick lis) {
        this.lis = lis;
    }

    public void refreshAllData(List<T> list) {
        this.lists.clear();
        if (list != null) {
            this.lists.addAll(list);
        }
        this.notifyDataSetChanged();
    }

    @Override
    public abstract V onCreateViewHolder(ViewGroup container, int type);

    @Override
    public abstract void onBindViewHolder(V holder, int position);


}
